package com.ynny.librarysystemmanager.controller;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.ynny.librarysystemmanager.common.R;
import com.ynny.librarysystemmanager.entity.User;
import com.ynny.librarysystemmanager.entity.UserDTO;
import com.ynny.librarysystemmanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")

    public R list( User user){

        List<User> users = userService.findAll(user);

        return R.ok(users);
    }

    @GetMapping("/page")

    public R page( UserDTO dto){

        PageInfo pageInfo = userService.page(dto);

        return R.ok(pageInfo);
    }

    @PutMapping("/edit")
    public R update(@RequestBody User user){

        if (ObjectUtil.isEmpty(user.getUserId())){
            int result = userService.addUser(user);
        }else {
            int result = userService.updateUser(user);
        }


        return R.ok(user);
    }



    //禁用用户  1 禁用   0可用
    @PostMapping("/disable/{userId}/{status}")
    public R disable(@PathVariable("userId") Integer userId ,
                     @PathVariable("isDelete") Integer status){

        int result = userService.disableUser(userId , status);

        return R.ok(result);
    }




}
